package com.siloam.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.siloam.module.Article;

public class ArticleDao {

	private SqlSession sqlSession;

	public SqlSession getSqlSession() {
		return sqlSession;
	}

	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}

	private String getMapper(String method) {
		return "com.siloam.sqlmap.ArticleMapper." + method;
	}

	public void add(Article article){
		sqlSession.insert(getMapper("add"), article);
	}
	
	public List<Article> findUserArticle(int userId, int rowBeginIndex, int rowCount){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("rowBeginIndex", rowBeginIndex);
		params.put("rowCount", rowCount);
		return sqlSession.selectList(getMapper("findUserArticles"), params);
	}
	
	public int userArticleCount(int userId){
		return sqlSession.selectOne(getMapper("userArticleCount"), userId);
	}
	
	public Article load(int id){
		return sqlSession.selectOne(getMapper("load"), id);
	}
	
	public void delete(int id){
		sqlSession.delete(getMapper("deleteArticle"), id);
	}
	
	public void update(Article article){
		sqlSession.update(getMapper("update"), article);
	}
	
}
